﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace mtdsharp
{
    public partial class MasterWindow : Form
    {
        private GTFSDataSet gtfsData;
        private Size emblemSize;


        public MasterWindow()
        {
            InitializeComponent();
        }

        private void UpdateDeparturesListView()
        {
            if (StopComboBox.SelectedItem != null)
            {
                DeparturesListView.Items.Clear();
                DeparturesListView.LargeImageList.Images.Clear();

                Stop selected_stop = (Stop)StopComboBox.SelectedItem;

                List<Departure> departure_list = gtfsData.GetDepartures(selected_stop, fromTimePicker.Value, toTimePicker.Value);

                foreach (Departure d in departure_list)
                {
                    string emblemtext = d.RouteNumber.ToString() + d.Direction.Substring(0, 1);
                    DeparturesListView.LargeImageList.Images.Add(emblemtext, Utilities.GenerateEmblem(emblemSize.Width, emblemSize.Height, emblemtext, d.RouteColor, d.RouteTextColor));
                    DeparturesListView.Items.Add(d.Time.ToShortTimeString(), emblemtext);
                }
            }
        }

        private void MasterWindow_Load(object sender, EventArgs e)
        {
            gtfsData = new GTFSDataSet();

            DeparturesListView.LargeImageList = new ImageList();


            int height = DeparturesListView.TileSize.Height - 4;
            int width = (int)(height * 1.62);
            emblemSize = new Size(width, height);

            DeparturesListView.LargeImageList.ImageSize = emblemSize;

            fromTimePicker.Value = DateTime.Now;
            toTimePicker.Value = fromTimePicker.Value.AddHours(2.0);

            for (int i = 0; i < gtfsData.Stops.Count; i++)
            {
                StopComboBox.Items.Add(gtfsData.Stops[i]);
            }
        }

        private void StopComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateDeparturesListView();
        }

        private void fromNowButton_Click(object sender, EventArgs e)
        {
            fromTimePicker.Value = DateTime.Now;
        }

        private void toHoursButton_Click(object sender, EventArgs e)
        {
            toTimePicker.Value = fromTimePicker.Value.AddHours(2.0);
        }

        private void fromTimePicker_ValueChanged(object sender, EventArgs e)
        {
            if (fromTimePicker.Value.CompareTo(DateTime.Now) < 0)
            {
                fromTimePicker.Value = DateTime.Now;
            }
            UpdateDeparturesListView();
        }

        private void toTimePicker_ValueChanged(object sender, EventArgs e)
        {
            if (toTimePicker.Value.CompareTo(DateTime.Now.AddHours(24)) > 0)
            {
                toTimePicker.Value = DateTime.Now.AddHours(24);
            }
            UpdateDeparturesListView();
        }
    }
}
